package com.lkm.market2_0.m.dataUp;

import com.lkm.javatool.CollectionHelp;
import com.lkm.javatool.ServerTimeSync;
import com.lkm.javatool.StringHelp;
import com.lkm.market2_0.entity.StallEntity;
import com.lkm.market2_0.entity.SteelyardInfo;
import com.lkm.market2_0.help.RandomStringUtils;
import com.lkm.market2_0.m.steelyarddata.OrderEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Deprecated 
public class DataUpModuleImpl implements DataUpModule {
    private ThreadPoolExecutor THREAD_POOL_EXECUTOR;
    private static final int CORE_POOL_SIZE = 5 * 3;
    private static final int MAXIMUM_POOL_SIZE = 128 ;
    private static final int KEEP_ALIVE = 1 * 3;

    public DataUpModuleImpl() {
        BlockingQueue<Runnable> sPoolWorkQueue = new SynchronousQueue<Runnable>();
        THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(
                CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE,
                TimeUnit.SECONDS, sPoolWorkQueue,
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());
    }


    @Override
    public void upState(SteelyardInfo steelyardInfo, boolean isExceptions) {
        THREAD_POOL_EXECUTOR.execute(new Runnable() {
            @Override
            public void run() {
                upState_(steelyardInfo, isExceptions);
            }
        });
    }

    @Override
    public void submitWeightInfo(SteelyardInfo steelyardInfo, OrderEntity orderEntity) {


        THREAD_POOL_EXECUTOR.execute(new Runnable() {
            @Override
            public void run() {
                submitWeightInfo_(steelyardInfo, orderEntity);
            }
        });


    }

    
    public void upState_(SteelyardInfo steelyardInfo, boolean isExceptions) {
        SZUpdateStatus szWeightInfo = new SZUpdateStatus();
        szWeightInfo.macAddr = steelyardInfo.getSteelyardMac();
        szWeightInfo.deviceNo = steelyardInfo.getB_id();
        szWeightInfo.deviceModel = steelyardInfo.deviceModel;
        szWeightInfo.factoryName = "广州市中兴电子衡器厂";

        szWeightInfo.productionDate = steelyardInfo.deviceAge;

        szWeightInfo.userStatus = isExceptions ? 1 : 0;

        StallEntity stallEntity = steelyardInfo.getStallEntity();
        if (stallEntity != null) {
            szWeightInfo.stallCode = "" + stallEntity.getStallNo();
            szWeightInfo.businessEntity = stallEntity.businessentity;
            szWeightInfo.creditCode = stallEntity.creditcode;
        }

        SZApi.updateStatus(szWeightInfo, null, true);
    }


    private void submitWeightInfo_(SteelyardInfo steelyardInfo, OrderEntity orderEntity) {

        List<OrderEntity.GoodEntity> goodEntities = orderEntity.getGoods();
        if (goodEntities == null) return;

        if (orderEntity.totalPrice <= 0) {

            return;
        }
        if(CollectionHelp.isEmpty(goodEntities))return;

        List<SZWeightInfo> szWeightInfos = new ArrayList<>(goodEntities.size());

        for (OrderEntity.GoodEntity gn : goodEntities) {

            
            if (gn == null || gn.weight < 0.01 || gn.unitType == 2) {
                continue;
            }


            SZWeightInfo szWeightInfo = new SZWeightInfo();
            szWeightInfo.amounts = "" + orderEntity.totalPrice;

            szWeightInfo.macAddr = steelyardInfo.getSteelyardMac();
            szWeightInfo.deviceNo = steelyardInfo.getB_id();
            szWeightInfo.deviceModel = steelyardInfo.deviceModel;
            szWeightInfo.factoryName = "广州市中兴电子衡器厂";

            szWeightInfo.productionDate = steelyardInfo.deviceAge;


            szWeightInfo.orderNo = orderEntity.orderNum;

            if (StringHelp.isEmpty(szWeightInfo.orderNo)) {
                szWeightInfo.orderNo = ServerTimeSync.getInstance().getServerCurTime() + RandomStringUtils.random(4, RandomStringUtils.azAZ90);
            }

            szWeightInfo.goodsCode = gn.goodId;
            szWeightInfo.goodsName = gn.goodName;

            szWeightInfo.orderTime = orderEntity.createTime;
            szWeightInfo.initAd = gn.zeroAD;
            szWeightInfo.zeroAd = gn.offsetZeroAD;


            szWeightInfo.weightAd = gn.ad ;

            szWeightInfo.weight = "" + gn.weight;
            szWeightInfo.price = "" + gn.unitPrice;

            StallEntity stallEntity = steelyardInfo.getStallEntity();
            if (stallEntity != null) {
                szWeightInfo.stallCode = "" + stallEntity.getStallNo();
                szWeightInfo.businessEntity = stallEntity.businessentity;
                szWeightInfo.creditCode = stallEntity.creditcode;
            }
            szWeightInfos.add(szWeightInfo);

        }
//        System.out.println("====szWeightInfos "+new Gson().toJson(szWeightInfos));
        if (!szWeightInfos.isEmpty()) {

            boolean isok = true;
            for (int i = 0; i < szWeightInfos.size(); i++) {
                SZWeightInfo szWeightInfo = szWeightInfos.get(i);
                if(szWeightInfo.weightAd<=0||szWeightInfo.zeroAd<=0||szWeightInfo.initAd<=0){
                    isok=false;
                    break;
                }
            }

            if(isok)
            SZApi.submitWeightInfo(steelyardInfo, szWeightInfos.toArray(new SZWeightInfo[szWeightInfos.size()]), goodEntities.get(0), steelyardInfo.getStallEntity(), null, true);
        }
    }
}
